package cn.bob.org.controller;

import cn.bob.basic.anno.Aopanno;
import cn.bob.basic.anno.PreAuthorize;
import cn.bob.basic.query.BaseQuery;
import cn.bob.basic.utils.AjaxResult;
import cn.bob.basic.utils.PageList;
import cn.bob.org.domain.Department;
import cn.bob.org.domain.Employee;
import cn.bob.org.query.DepartmentQuery;
import cn.bob.org.service.IDepartmentService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author bob
 * @data 2022/9/30-9:17
 */
@RestController  //设置这个类返回结果为json格式 数据
@RequestMapping("/dept")
@Aopanno
@PreAuthorize
public class DepartmentController {
    @Autowired
    private IDepartmentService service;
    //查询所有数据
    @GetMapping
    @PreAuthorize(name ="查询所有部门数据",sn = "department:findAll")
    public List<Department> findAll(){
        List<Department> all = service.findAll();
        return all;
    }
    //查询单条数据
    @GetMapping("/{id}")
    @PreAuthorize(name ="按照id查询部门数据",sn = "department:findById")
    public Department findById(@PathVariable("id") Long id){
        return service.findById(id);
    }
    //根据id删除数据
    @DeleteMapping("/{id}")
    @Aopanno(method = "删除操作",methodDoc = "删除部门表的数据")
    @PreAuthorize(name ="按照id删除部门数据",sn = "department:delById")
    public AjaxResult delById(@PathVariable("id") Long id){
        service.delById(id);
        return AjaxResult.me().success();
    }
    //批量删除数据
    @PatchMapping
    @Aopanno(method = "批量删除操作",methodDoc = "批量删除了部门表的数据")
    @PreAuthorize(name ="批量删除部门数据",sn = "department:batchDel")
    public  AjaxResult batchDel(@RequestBody List<Long> ids){
        service.batchDel(ids);
        return  AjaxResult.me().success();
    }
    //添加与修改
    @PutMapping
    @Aopanno(method = "添加与修改操作",methodDoc = "添加与修改部门表的数据")
    public AjaxResult addorup(@RequestBody Department department){
        if (null == department.getId()){
            service.add(department);
        }else {
            service.update(department);
        }
        return AjaxResult.me().success();
    }
    //分页查询
    @PostMapping
    @PreAuthorize(name ="分页查询部门数据",sn = "department:queryByPage")
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query){
        System.out.println(query.getCurrentPage());
        System.out.println(query.getPageSize());
        return service.queryByPage(query);
    }
    //部门树查询
    @GetMapping("/deptTree")
    @PreAuthorize(name ="查询部门树",sn = "department:tree")
    public  List<Department> deptTree(){
        return service.deptTree();
    }


}
